
import inspect
import AccessSqlite3 as db



# --- Private function ---

def getClassByName(className):
    if className in globals():
        return globals()[className]
    return None



# --- Public function ---

def get(key):
    result = db.GetEntry(key.tableName, key.items)
    if [] != result:
        returnDicts = []
        itemNames = result[0]
        for entries in result[1:]:
            keyEntry = {}
            for itemName, value in zip(itemNames, entries):
                keyEntry[itemName] = value
            returnDicts.append(keyEntry)
        return returnDicts
    return []

def delete(key):
    db.DeleteEntry(key.tableName, key.items)
    return



# --- Private class ---

class Property(object):
     def __init__(self, name=None, default=None, required=False):
        self.name = name
        self.default = default
        self.required = required
        super(Property, self).__init__()
        return




# --- Public class ---

class Key():
    def __init__(self, tableName, **kwargs):
        self.tableName = tableName
        self.items = kwargs

class Model(object):
    def delSysAttribFromDict(self, dict):
        sysAttribList = ['__module__', '__doc__', 'func_doc']
        for att in sysAttribList:
            if att in dict:
                del dict[att]
        return dict

    def __init__(self, *args, **kwargs):
        # get all propertyTypes
        propertyTypes = self.__class__.__dict__.copy()
        self.delSysAttribFromDict(propertyTypes)

        # init properties
        self.entry = {}
        self.Properties = {}
        for propertyName in propertyTypes.keys():
            if propertyName in kwargs:
                value = kwargs[propertyName]
            else:
                if 'default' in propertyTypes[propertyName].__dict__:
                    value = propertyTypes[propertyName].__dict__['default']
                else:
                    value = None
            if 'name' in propertyTypes[propertyName].__dict__:
                propertyTypes[propertyName].__dict__['name'] = propertyName
            #propertyTypes[propertyName].__dict__['value'] = value
            cls = getClassByName(propertyTypes[propertyName].__class__.__name__)
            obj = cls(propertyTypes[propertyName].__dict__)
            self.Properties[propertyName] = obj

            self.entry[propertyName] = {'value': value, 'type': propertyTypes[propertyName].type, 'required': propertyTypes[propertyName].required}
            setattr(self, propertyName, value)

        super(Model, self).__init__()
        return

    def put(self):
        for item in self.entry:
            value = getattr(self, item)
            if isinstance(value, Property): # deleted, value is the class attr, not object attr
                self.entry[item]['value'] = None
            else:
                self.entry[item]['value'] = value
        db.AddEntry(self.__class__.__name__, self.entry)
        return

    def drop(self):
        db.DropTable(self.__class__.__name__)
        pass


class IntegerProperty(Property):
     def __init__(self, name=None, default=None, required=False):
        self.type = int
        super(IntegerProperty, self).__init__(name=name, default=default, required=required)
        return


class FloatProperty(Property):
     def __init__(self, name=None, default=None, required=False):
        self.type = float
        super(FloatProperty, self).__init__(name=name, default=default, required=required)
        return


class StringProperty(Property):
     def __init__(self, name=None, default=None, required=False):
        self.type = str
        super(StringProperty, self).__init__(name=name, default=default, required=required)
        return

'''
class TextProperty(Property):
     def __init__(self, name=None, default=None, required=False):
        self.type = str
        super(TextProperty, self).__init__(name=name, default=default, required=required)
        return


class BlobProperty(Property):
     def __init__(self, name=None, default=None, required=False):
        super(BlobProperty, self).__init__(name=name, default=default, required=required)
        return
'''





